// 03myCenter/02sectorization/01myChange/recharge/recharge.js
var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this

Page({
    /*页面的初始数据 */
    data: {
        isInput: 0,
        money:''
    },

    /*生命周期函数--监听页面加载 */
    onLoad: function (options) {
        _this = this
    },
    // 输入充值金额
    money: function(e){
        var mon = e.detail.value
        var m
        if (co.empty(mon)){
            m = 0
        }else{
            m = 1
        }
        this.setData({
            isInput: m,
            money: mon
        })
    },
    // 下一步
    next: function(){
        if (this.data.isInput == 1){
            this.weXinPay(this.data.money)
        }
    },
    // 微信支付
    weXinPay: function (money) {
        //购买分销商金额
        var param = {
            openid: app.Data.userInfo.openid,
            appid: app.Data.app.appid,
            key: 'sUiYuAnTeChNoLoGyHtTpSwWwLs293cC',
            notify_url: 'http://api.igoyp.com/public/weixinpay/notify.php',
            mch_id: '1480163852',
            money: money,
            abstract: '账户充值'
        }
        co.getShareApi('api/Wxpay/createParam', param, function (res) {
            res.data.success = function (result) {
                _this.moneyRechargeConsume(100)
                console.log('【调起支付成功res】', result)
            }
            res.data.fail = function (result) {
                console.log('【调起支付失败res】', result)
            }
            //调起微信支付
            wx.requestPayment(res.data)
        })
    },
    // 充值接口
    moneyRechargeConsume: function (money) {
        var param = {
            User_ID: app.Data.userInfo.User_ID,
            Amount: money,
            Note: '余额充值'
        }
        co.getShareApi('user/Fmg/moneyRechargeConsume', param, function (res) {
            if (res.code == 0) {
                app.getWXUserInfo(function(res){
                    wx.navigateBack({})
                })
            } else {
                console.log("充值失败")
            }
        })
    },
})